package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Auther：DONG on 2021/11/2
 **/
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;


    @Override
    public ResponseResult findByListPage(SensitiveDto dto) {
        if (null==dto){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        Page<AdSensitive> sensitivePage = new Page<>(dto.getPage(),dto.getSize());
        IPage<AdSensitive> adSensitiveResult = this.page(sensitivePage, queryWrapper);
        if (null==adSensitiveResult ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

        }
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) adSensitiveResult.getTotal());
        pageResponseResult.setData(adSensitiveResult.getRecords());
    return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.参数校验
        if (null==adSensitive){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.参数填充
        adSensitive.setCreatedTime(new Date());
        //3.调用接口实现数据库插入
        boolean saveResult = this.save(adSensitive);
        if (!saveResult){
            return  ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FILE);
        }
        //4.返回前台

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
        //1.参数校验
        if (null==adSensitive||null== adSensitive.getId()){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.填充参数  无
        //3.调用接口实现修改数据库
        boolean updateResult = this.updateById(adSensitive);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FILE);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //1.参数校验
        if (null== id){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive adSensitive = this.getById(id);
        if (null==adSensitive){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //调用接口实现数据库的删除
        boolean deleteResult = this.removeById(id);
        if (!deleteResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FILE);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
